package com.example.controller;

import com.example.entity.User;
import com.example.service.UserService;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户API控制器
 * 提供用户数据的增删改查操作，并返回数据库信息
 */
@RestController
@RequestMapping("/api/users")
public class UserApiController {

    @Resource
    private UserService userService;
    
    @Resource
    private JdbcTemplate jdbcTemplate;

    /**
     * 创建用户
     * @param user 用户信息
     * @return 响应结果
     */
    @PostMapping("/create")
    public Map<String, Object> createUser(@RequestBody User user) {
        Map<String, Object> result = new HashMap<>();
        
        try {
            // 获取当前数据库名称
            String dbName = jdbcTemplate.queryForObject("SELECT DATABASE()", String.class);
            
            // 保存用户
            boolean success = userService.save(user);
            
            result.put("success", success);
            result.put("message", success ? "用户创建成功" : "用户创建失败");
            result.put("database", dbName);
            result.put("data", user);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "用户创建失败: " + e.getMessage());
        }
        
        return result;
    }
    
    /**
     * 根据ID查询用户
     * @param id 用户ID
     * @return 用户信息和数据库信息
     */
    @GetMapping("/query/{id}")
    public Map<String, Object> queryUserById(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        
        try {
            // 获取当前数据库名称
            String dbName = jdbcTemplate.queryForObject("SELECT DATABASE()", String.class);
            
            // 查询用户
            User user = userService.getById(id);
            
            result.put("success", true);
            result.put("database", dbName);
            result.put("data", user);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败: " + e.getMessage());
        }
        
        return result;
    }
    
    /**
     * 查询所有用户
     * @return 用户列表和数据库信息
     */
    @GetMapping("/query/all")
    public Map<String, Object> queryAllUsers() {
        Map<String, Object> result = new HashMap<>();
        
        try {
            // 获取当前数据库名称
            String dbName = jdbcTemplate.queryForObject("SELECT DATABASE()", String.class);
            
            // 查询所有用户
            List<User> users = userService.list();
            
            result.put("success", true);
            result.put("database", dbName);
            result.put("data", users);
            result.put("count", users.size());
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败: " + e.getMessage());
        }
        
        return result;
    }
    
    /**
     * 测试数据库分片
     * @return 数据库信息
     */
    @GetMapping("/test-sharding")
    public Map<String, Object> testSharding() {
        Map<String, Object> result = new HashMap<>();
        
        try {
            // 获取当前数据库名称
            String dbName = jdbcTemplate.queryForObject("SELECT DATABASE()", String.class);
            // 获取数据库版本
            String version = jdbcTemplate.queryForObject("SELECT VERSION()", String.class);
            
            result.put("success", true);
            result.put("database", dbName);
            result.put("version", version);
            result.put("timestamp", System.currentTimeMillis());
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败: " + e.getMessage());
        }
        
        return result;
    }
} 